<?php

function xAPProcessxAPBSCevent($msg){
	xAPProcessBSC($msg);
}
function xAPProcessxAPBSCinfo($msg){
	xAPProcessBSC($msg);
}

$config["xAPInc"][]=array("class"=>"xapbsc.info","function"=>"xAPProcessBSC"); // register receiver
$config["xAPInc"][]=array("class"=>"xapbsc.event","function"=>"xAPProcessBSC"); // register receiver

function xAPProcessBSC($msg){
//get values
	foreach($msg["body"] as $Key=>$Value){

			$state=strtoupper($Value["state"]);
			$level=$Value["level"];
			$levelmax="";
			if (strpos($level,"/")>0){
				$levelmax=substr($level,strpos($level,"/")+1);
				$level=substr($level,0,strpos($level,"/"));
			}
			$text=$Value["text"];
			$dtext=$Value["displaytext"];
	}

//get current state
	$sql="select xValue,xValue2,xValue3,xValue4,xValue5,DeviceID,Log from  xAP_device where 
		source='".dbesc(strtolower($msg["header"]["source"]))."' and 
		class='BSC' and 
		section='BSC' and 
		uid='".dbesc(strtoupper($msg["header"]["uid"]))."' and 
		xitem='BSC' and
		xType=2";
	$results=dbquery($sql);
	if(isset($results[0]["xValue"])){
		$sql="update xAP_device set 
				xValue='".dbesc($state)."', 
				xValue2='".dbesc($level)."', 
				xValue3='".dbesc($levelmax)."', 
				xValue4='".dbesc($text)."', 
				xValue5='".dbesc($dtext)."', 
				Updated='".date("Y-m-d H:i:s")."' where 
				deviceID='".dbesc($results[0]["DeviceID"])."'";
			dbexec($sql);
			xAPLog("xAP","BSCUPDATE ".$msg["header"]["source"],"$state:$level:$levelmax:$text");
	}
	else
	{
		$sourcemain=$msg["header"]["source"];
		$sourcesub="";
		if(strpos($msg["header"]["source"],":")>0){
			$sourcemain=substr($msg["header"]["source"],0,strpos($msg["header"]["source"],":"));
			$sourcesub=substr($msg["header"]["source"],strpos($msg["header"]["source"],":")+1);
		}
		$sql="insert into xAP_Device 
		(DeviceID,UID,Source,SourceMain,SourceSub,Class,Section,xItem,xValue,xValue2,xValue3,xValue4,xValue5,Log,Created,Updated,xType) 
		VALUES (
		Null,
		'".dbesc(strtoupper($msg["header"]["uid"]))."',
		'".dbesc(strtolower($msg["header"]["source"]))."',
		'".dbesc($sourcemain)."',
		'".dbesc($sourcesub)."',
		'BSC',
		'BSC',
		'BSC',
		'".dbesc(($state))."',
		'".dbesc(($level))."',
		'".dbesc(($levelmax))."',
		'".dbesc(($text))."',
		'".dbesc(($dtext))."',
		1,
		'".date("Y-m-d H:i:s")."',
		'".date("Y-m-d H:i:s")."',
		2
		)";
		dbexec($sql);
		xAPLog("xAP","BSCINSERT ".$msg["header"]["source"],"$state:$level:$levelmax:$text");
		//defaults
		$results[0]["Log"]=1;
	}

	// do things if changed
	if(($results[0]["xValue"]!=$state || $results[0]["xValue2"]!=$level || $results[0]["xValue4"]!=$text ) ){
	// log message?
		if($results[0]["Log"]==1 && intval($results[0]["DeviceID"])>0){
			$sql="insert into xAP_Device_Log (LogID,DeviceID,xValue,xValue2,xValue3,xValue4,xValue5,Created) VALUES (
			Null,
			'".dbesc($results[0]["DeviceID"])."',
			'".dbesc(($state))."',
			'".dbesc(($level))."',
			'".dbesc(($levelmax))."',
			'".dbesc(($text))."',
			'".dbesc(($dtext))."',
			'".date("Y-m-d H:i:s")."'
			)";
			dbexec($sql);
			xAPLog("xAP","BSCLOG ".$msg["header"]["source"],"$state:$level:$levelmax:$text");

		}
	}
}

?>